package com.example.androidbighomework.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.androidbighomework.R;
import com.example.androidbighomework.bean.HomeDataBean;

public class myBaseAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private View view;
    private ListView mListViewHome;
    //要绑定的条目的数目
    @Override
    public int getCount() {
        return HomeDataBean.getInstance().getHomeContentText().length;
    }
    //根据一个索引（位置）获得该位置的对象
    @Override
    public Object getItem(int position) {
        return HomeDataBean.getInstance().getHomeContentText()[position];
    }
    //获取条目的id
    @Override
    public long getItemId(int position) {
        return position;
    }
    //获取该条目要显示的界面
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder holder;
        //先判断convertView是否为空，是的话就创建ViewHolder，不是的话就取出ViewHolder
        if(convertView==null){
            //根据xml布局文件来实例化相应的View
            convertView=view.inflate(parent.getContext(), R.layout.list_home_item_layout,null);
            holder=new ViewHoder();
            holder.image=convertView.findViewById(R.id.list_iv_img);
            holder.content=convertView.findViewById(R.id.list_tv_content);
            convertView.setTag(holder);
        }
        else{
            holder=(ViewHoder) convertView.getTag();
        }
        //设置随机数（0-3）取HomeDataBean里的图片用
        int a=0;
        int b=3;
        int random =(int) (Math.random()*(b-a+1) + a);

        //给控件赋值
        holder.content.setText(HomeDataBean.getInstance().getHomeContentText()[position]);
        holder.image.setBackgroundResource(HomeDataBean.getInstance().getHomeContentImage()[position]);
        //holder.image.setImageDrawable(HomeDataBean.getInstance().getHomeContentImage()[random]);
        return convertView;
    }
    //标记我们的控件
    class ViewHoder{
        ImageView image;
        TextView content;
    }
}
